package com.zzyl.nursing.controller.member;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.BindFamilyMemberDto;
import com.zzyl.nursing.dto.DeviceDataQueryDto;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.DeviceDataDayVo;
import com.zzyl.nursing.dto.LoginUserDto;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.FamilyBindElderVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.LoginUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人家属Controller
 * 
 * @author mr.wu
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/member")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;


    @ApiOperation("小程序登录")
    @PostMapping("/user/login")
    public AjaxResult login(@RequestBody LoginUserDto dto) {
        LoginUserVo loginUserVo = familyMemberService.login(dto);
        return success(loginUserVo);
    }

    /**
     * 按周统计查询指标数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation("按周统计查询指标数据")
    @GetMapping("/user/queryDeviceDataListByWeek")
    public AjaxResult queryDeviceDataListByWeek(
            @RequestParam("iotId") String iotId,
            @RequestParam("functionId") String functionId,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime){
        List<DeviceDataDayVo> list=familyMemberService.queryDeviceDataListByWeek(iotId,functionId,startTime,endTime);
        return success(list);
    }


//    /**
//     * 查询老人家属列表
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:list')")
//    @GetMapping("/list")
//    @ApiOperation(value = "获取老人家属列表")
//    public TableDataInfo<List<FamilyMember>> list(FamilyMember familyMember)
//    {
//        startPage();
//        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出老人家属列表
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:export')")
//    @Log(title = "老人家属", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出老人家属列表")
//    public void export(HttpServletResponse response, FamilyMember familyMember)
//    {
//        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
//        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
//        util.exportExcel(response, list, "老人家属数据");
//    }
//
//    /**
//     * 获取老人家属详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取老人家属详细信息")
//    public R<FamilyMember> getInfo(@ApiParam(value = "老人家属ID", required = true)
//            @PathVariable("id") Long id)
//    {
//        return R.ok(familyMemberService.selectFamilyMemberById(id));
//    }
//
//    /**
//     * 新增老人家属
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:add')")
//    @Log(title = "老人家属", businessType = BusinessType.INSERT)
//    @PostMapping
//    @ApiOperation("新增老人家属")
//    public AjaxResult add(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyMember familyMember)
//    {
//        return toAjax(familyMemberService.insertFamilyMember(familyMember));
//    }
//
//    /**
//     * 修改老人家属
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:edit')")
//    @Log(title = "老人家属", businessType = BusinessType.UPDATE)
//    @PutMapping
//    @ApiOperation("修改老人家属")
//    public AjaxResult edit(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyMember familyMember)
//    {
//        return toAjax(familyMemberService.updateFamilyMember(familyMember));
//    }
//
//    /**
//     * 删除老人家属
//     */
//    @PreAuthorize("@ss.hasPermi('nursing:member:remove')")
//    @Log(title = "老人家属", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    @ApiOperation("删除老人家属")
//    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
//    {
//        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
//    }

    /**
     * 绑定家人
     */
    @ApiOperation("绑定家人")
    @PostMapping("/user/add")
    public AjaxResult bindFamilyMember(@RequestBody BindFamilyMemberDto dto) {
        int rows = familyMemberService.bindFamilyMember(dto);
        return rows > 0 ? success() : error("绑定家人失败");
    }

    /**
     *  查询家人列表
     */
    @ApiOperation("查询家人列表")
    @GetMapping("/user/my")
    public AjaxResult myFamilyList() {
        List<FamilyBindElderVo> list = familyMemberService.selectMyFamilyList();
        return AjaxResult.success(list);
    }

    /**
     * 查看家人列表 - 分页查询
     */
    @ApiOperation("查看家人列表")
    @GetMapping("user/list-by-page")
    public AjaxResult listByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 获取用户id
        Long userId = UserThreadLocal.getUserId();
        // Long userId = getLoginUser().getUserId();
        Page<FamilyMemberElderVo> page = familyMemberService.selectFamilyMemberElderPage(pageNum, pageSize, userId);
        return  AjaxResult.success(page.getRecords());
        // return getDataTable(page);
    }

    /**
     * 4. 按天统计查询指标数据
     */
    @ApiOperation("按天统计查询指标数据")
    @GetMapping("/user/queryDeviceDataListByDay")
    public AjaxResult queryDeviceDataListByDay(DeviceDataQueryDto queryDto) {
        return AjaxResult.success(familyMemberService.queryDeviceDataListByDay(queryDto));
    }

    @ApiOperation("查询健康数据")
    @PostMapping("user/QueryDevicePropertyStatus")
    public AjaxResult QueryDevicePropertyStatus(@RequestBody  DeviceDto dto) {

        Map<String,Object> resultMap = familyMemberService.getDevicePropertyStatus(dto.getDeviceName());
        return AjaxResult.success(resultMap);
    }


}
